<?php

namespace App\Http\Controllers\Admin\Integral;

use App\Http\Controllers\Controller;
use App\Models\LiveRoom;
use App\Traits\Controllers\AdminTraits;
use App\Transformers\Live\LiveRoomTransformer;
use Carbon\Carbon;
use Illuminate\Http\Request;

class LiveRoomsController extends Controller
{
    //使用增删改查的方法
    use AdminTraits;

    //使用直播间的模型
    public $model = LiveRoom::class;
    //使用直播间的transformer
    public $transformer = LiveRoomTransformer::class;

    //表单的验证
    public function rules(Request $request): array
    {
        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'POST':
                return [];
                break;
            case 'PUT':
            case 'PATCH':
                return [  // between:2,5 验证一个字段的长度必须在 2,5之间
                    'anchor_id' => 'required',
                    'name' => 'required',
                    'cover_img' => 'required',
                    'share_img' => 'required|',
                    'phone' => ['required', 'regex:/^1[3-9]\d{9}$/'],
                    'start_at' => 'required',
                    'end_at' => 'required',
                    'screen_type' => 'required',
                    'room_type' => 'required',
                    'is_openLike' => 'required',
                    'is_openShelf' => 'required',
                    'is_openComments' => 'required'
                ];
                break;
        }
        return [];
    }

    //返回验证的信息
    public function ruleMessages(): array
    {
        return [
            'anchor_id.required' => '请选择主播',
            'name.required' => '请填写直播间名称',
            'cover_img.required' => '请选择背景图片',
            'cover_img.image' => '请选择正确的图片格式',
            'share_img.required' => '请选择分享图片',
            'share_img.image' => '请选择正确的图片格式',
            'phone.required' => '请选择主播的联系电话',
            'phone.regex' => '请输入正确的手机号',
            'start_at.required' => '请选择直播时间',
            'end_at.required' => '请选择直播时间',
            'verticalScreen.required' => '请选择显示的样式',
            'mobilePhone.required' => '请选择直播间类型',
            'is_openLike.required' => '请选择是否开启点赞',
            'is_openShelf.required' => '请选择是否开启直播卖货',
            'is_openComments.required' => '请选择是否开启评论',
        ];
    }


    //显示直播间列表
    public function index(Request $request)
    {
        //先处理传过来的搜索数据
        $this->formatIndexQuery($request);

        $data = $this
            ->getQuery()
            ->pimp()//自动转换where条件
            ->with('liveAnchor:id,wechat,name')
            ->paginate($this->getPageLimit());
        //进行数据返回
        return $this->response->paginator($data, resolve($this->transformer));
    }

    //使用更新的方法
    public function update($id, Request $request)
    {
        //验证用户凭证信息
        $this->validateData($request);

        $data = $this->getQuery()->findOrFail($id);

        if ($this->policy) {
            $this->authorize('update', $data);
        }

        $data->fill($request->all());
        $data->save();

        return $this->response->noContent();
    }

}
